package com.uduemc.biso.module.basic.service.common.controller;

import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.uduemc.biso.core.common.extities.CCustomerData;
import com.uduemc.biso.core.common.extities.CHostListCustomerData;
import com.uduemc.biso.core.utils.RestResult;
import com.uduemc.biso.module.basic.service.common.service.CCustomerService;

/**
 * 客户服务的提供者
 * 
 * @author guanyi
 *
 */
@RestController
@RequestMapping("/common/customer")
public class CCustomerController {

	private final static Logger logger = LoggerFactory.getLogger(CCustomerController.class);

	@Autowired
	private CCustomerService cCustomerServiceImpl;

	/**
	 * 新增 客户以及其主机信息
	 * 
	 * @return
	 */
	@PostMapping("/append")
	public RestResult append(@Valid @RequestBody CCustomerData customerData) {
		if (customerData == null || customerData.getCustomerUser() == null || customerData.getHost() == null) {
			return RestResult.error();
		}
		CCustomerData append = cCustomerServiceImpl.append(customerData);
		return RestResult.ok(append);
	}

	/**
	 * 通过id 以及 agentId 获取 CHostListCustomerData数据
	 * 
	 * @return
	 */
	@GetMapping("/info/{id:\\d+}/{agentId:\\d+}")
	public RestResult info(@PathVariable("id") long id, @PathVariable("agentId") long agentId) {
		CHostListCustomerData info = cCustomerServiceImpl.info(id, agentId);
		if (info == null) {
			return RestResult.noData();
		}
		return RestResult.ok(info);
	}

	/**
	 * 条件查询获取CHostListCustomerData数据
	 * 
	 * @param agentId
	 * @param username
	 * @param numberKey
	 * @param status
	 * @param pageNumber
	 * @param pageSize
	 * @return
	 */
	@PostMapping("/infos")
	public RestResult infos(@RequestParam("agentId") long agentId,
			@RequestParam(required = false, value = "username", defaultValue = "") String username,
			@RequestParam(required = false, value = "numberKey", defaultValue = "") String numberKey,
			@RequestParam(required = false, value = "status", defaultValue = "-1") short status,
			@RequestParam(required = false, value = "pageNumber", defaultValue = "1") int pageNumber,
			@RequestParam(required = false, value = "pageSize", defaultValue = "12") int pageSize) {

		logger.info("@RequestParam(): agentId: " + agentId + ", username: " + username + ", numberKey: " + numberKey
				+ ", status: " + status + ", pageNumber: " + pageNumber + ", pageSize: " + pageSize);

		PageInfo<CHostListCustomerData> infos = cCustomerServiceImpl.infos(agentId, username, numberKey, status,
				pageNumber, pageSize);
		return RestResult.ok(infos);
	}

}
